/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #include <stdio.h>
 #include <unistd.h>
 
 #include "ohos_init.h"
 #include "cmsis_os2.h"
 #include "iot_gpio.h"
 #include "iot_pwm.h"
 #include "iot_errno.h"
 #include "hi_io.h"
 
 #define RED_LED_PIN_NAME 2
 #define RED_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_10_GPIO
 #define GREEN_LED_PIN_NAME 8
 #define BLUE_LED_PIN_NAME 7
 
 #define IOT_PWM_DUTY_MAX  100
 #define PWM_FREQ_DIVITION 64000
 #define DELAY_US 250000
 #define STACK_SIZE (4096)
 #define PWM_PORT_NUM_RED  (2)
 #define PWM_PORT_NUM_GREEN  (0)  
 #define PWM_PORT_NUM_BLUE  (1)
 #define DOUBLE  (2)

 #define IOT_GPIO_KEY     14
 #define DELAY_TICKS_30  (30)

 enum LedState {
    LED_ON = 0,
    LED_OFF,
    LED_SPARK,
};

enum LedState g_ledState = LED_SPARK;
static int g_count = 100;
 
 static void PWMLedDemoTask(void)
 {
         // 炫彩灯板的红灯
         hi_io_set_func(RED_LED_PIN_NAME, HI_IO_FUNC_GPIO_10_PWM1_OUT);
         hi_io_set_func(GREEN_LED_PIN_NAME, HI_IO_FUNC_GPIO_10_PWM1_OUT);
         hi_io_set_func(BLUE_LED_PIN_NAME, HI_IO_FUNC_GPIO_10_PWM1_OUT);
         IoTPwmInit(PWM_PORT_NUM_RED);
         IoTPwmInit(PWM_PORT_NUM_GREEN);
         IoTPwmInit(PWM_PORT_NUM_BLUE);


     while (g_count) {
         switch (g_ledState) {
             case LED_ON:
                IoTPwmStart(PWM_PORT_NUM_RED, IOT_PWM_DUTY_MAX, PWM_FREQ_DIVITION);
                 break;
             case LED_OFF:
                 IoTPwmStop(PWM_PORT_NUM_RED);
                 IoTPwmStop(PWM_PORT_NUM_GREEN);
                 IoTPwmStop(PWM_PORT_NUM_BLUE);
                 break;
             case LED_SPARK:
             // use PWM control RED LED brightness
                for (int i = 1; i < IOT_PWM_DUTY_MAX; i *= DOUBLE) {
                 IoTPwmStart(PWM_PORT_NUM_RED, i, PWM_FREQ_DIVITION);
                 usleep(DELAY_US);
                 IoTPwmStop(PWM_PORT_NUM_RED);
                }
             
             //Green
            // use PWM control Green LED brightness
                for (int i = 1; i < IOT_PWM_DUTY_MAX; i *= DOUBLE) {
                IoTPwmStart(PWM_PORT_NUM_GREEN, i, PWM_FREQ_DIVITION);
                usleep(DELAY_US);
                IoTPwmStop(PWM_PORT_NUM_GREEN);
                }
    
            //Blue
            // use PWM control Blue LED brightness
                for (int i = 1; i < IOT_PWM_DUTY_MAX; i *= DOUBLE) {
                IoTPwmStart(PWM_PORT_NUM_BLUE, i, PWM_FREQ_DIVITION);
                usleep(DELAY_US);
                IoTPwmStop(PWM_PORT_NUM_BLUE);
                }
                 break;
             default:
                 osDelay(DELAY_TICKS_30);
                 break;
         }
     }
 }

 static void OnButtonPressed(void)
 {
     enum LedState nextState = LED_SPARK;
     switch (g_ledState) {
         case LED_ON:
             nextState = LED_OFF;
             break;
         case LED_OFF:
             nextState = LED_ON;
             break;
         case LED_SPARK:
             nextState = LED_OFF;
             break;
         default:
             break;
     }
 
     g_ledState = nextState;
     g_count--;
 }

 static void PWMLedDemo(void)
 {
     osThreadAttr_t attr;
 
     // set LED pin to GPIO function
     IoTGpioInit(RED_LED_PIN_NAME);
     IoTGpioInit(GREEN_LED_PIN_NAME);
     IoTGpioInit(BLUE_LED_PIN_NAME);

     IoTGpioSetDir(2, IOT_GPIO_DIR_OUT);
     IoTGpioSetDir(8, IOT_GPIO_DIR_OUT);
     IoTGpioSetDir(7, IOT_GPIO_DIR_OUT);

     IoTGpioRegisterIsrFunc(IOT_GPIO_KEY, IOT_INT_TYPE_EDGE,
        IOT_GPIO_EDGE_FALL_LEVEL_LOW,
        OnButtonPressed, NULL);
 
     attr.name = "PWMLedDemoTask";
     attr.attr_bits = 0U;
     attr.cb_mem = NULL;
     attr.cb_size = 0U;
     attr.stack_mem = NULL;
     attr.stack_size = STACK_SIZE;
     attr.priority = osPriorityNormal;
 
     if (osThreadNew(PWMLedDemoTask, NULL, &attr) == NULL) {
         printf("[ColorfulLightDemo] Falied to create PWMLedDemoTask!\n");
     }
 }
 APP_FEATURE_INIT(PWMLedDemo);
 